﻿$(function() {
	
    var countdown=60;
    var basepath=$("#basepath").val();
    var orgcode=$("#orgcode").val();
    
    //身份证读取	身份证读取成功，查询信息。
    // function card_read(){
    $("#card_read").click(function(){
        /*精伦身份证读卡器*/
       var flag =  Idcard();
       
       if (!flag) {
    	   return;
       }
       
        console.log("card_read end");
       // $("#sfzdiv").removeClass("hide");
        
        
        var idcard = $('#span_idcard').text();
        
        if(idcard){
        	 $(document.body).append("<div class='alert fade in alert_warn_sfz' id='btn_warn_dd'><div class='warn_div_sfz'><font class='font18' id='warn'>正在读取身份证信息，请稍候...</font></div></div>");  	
        	 validate_idcard(idcard);
        } else {
        	toastr.warning('请重新读取身份证或到人工窗口办理!');
        }
        
    });

    //发送验证码
    $("#send").click(function(){
        //验证码置空
        $("#dy_validation").val("");
        
        //验证手机号格式
       // if(validate_phone()){
            var phone = $("#mobileNumber").val();
            var idCard = $("#span_idcard").text();
            
            $.ajax({
                cache: true,
                type:"POST",
                url:basepath + "/print/sendMessage",
                dataType:"json",
                data:{idCard:idCard, phone:phone},
                success:function(data){
                	if (data.success) {
                		  var validationCode = data.data;
                          alert("验证码=" + validationCode);
                          
                          if(validationCode){
                              $("#validationCode").val(validationCode);
                              settime($("#send"));
                          }else{
                        	  toastr.warning("验证码发送失败, 请重新发送");
                          }
                		
                	} else {
                		toastr.warning(data.msg);
                	}
                }, error:function(){
                	toastr.warning("验证码发送失败, 请重新发送!");
                }
            });

       // }

    });
    
    //确定验证
    $("#confirm_").click(function(){
        validation();
    });

    //手机号验证
    /* $("#dy_phone").blur(function(){
        validate_phone();
    }); */
    $("#ceshi").click(
        function(){
            $("#div_key").attr("class","modal_key modal");
            $("#certNumber").val("370105196803292384");
            validate_idcard("370105196803292384");
        });

    $("#par").click(
        function(){
            $(document.body).append("<div id='span_' style='margin-top:200px;margin-left:900px;'>打印中</div>");
            //$("#progress_bar").append("<div class='alert fade in bar_warn' id='bar_close'><div  class='progress progress-striped active'><div class='bar' id='bar_dy' style='width: 1%;'></div></div><div class='bar_div' id='bar_div_dy'><font id='bar_font' class='font9'> 正在打印请稍后...</font></div></div>");
        }
    );
    
    //
    function validate_idcard(idcard){
    	//清空病案table
    	$("#table_banum").html("");
    	
        $.ajax({
        	cache:false,
            type:"GET",
            url:basepath+"/print/getMrRecordByIdCard",
            dataType:"json",
            data:{idCard : idcard},
            async: true, 
            dataType:"json",
            beforeSend: function(xhr){
            	
            },
            success:function(data){
            	if (data.success) {
            		 $("#btn_warn_dd").remove();
            		 
            		var basicDataList = data.data;
            		if (basicDataList == null || basicDataList.length == 0) {
                         toastr.warning('无归档病案!');
                         return;
            		}
            		
            		var phone = basicDataList[0].linkPhone;
            		
            		if (!phone) {
            			toastr.warning("预留病人手机号为空!");
            			return;
            		}
            		
            		if(!(/^1[3456789]\d{9}$/.test(phone))){
                        $("#btn_warn_dd").remove();
                        toastr.warning("预留病人手机号"+phone+"错误!");
                        return;
                    }
            		
            		$("#div_key").attr("class", "modal_key modal"); //显示数字按钮模态框
                    $("#crdiv").attr("class", "modal_ modal hide"); //隐藏获取读卡器身份证信息
                    $("#sfzdiv").attr("class", "modal_1"); //显示身份证信息
            
                    var span_phone = phone.substr(0, 3) + "****" + phone.substr(7, 4);
                    $("#span_phone").html(span_phone);
                    $("#mobileNumber").val(phone);
                    
                   // $("#link_phone").val(phone); 
                   // $("#dy_phone").val(phone);
            
                    $("#dy_validation").focus();
                    $("#choice").val("dy_validation");

                    fillTableMedical(basicDataList);
            	} else {
            		$("#btn_warn_dd").remove();
            		toastr.warning(data.msg);
            	}
            },
            error:function(){
            	$("#btn_warn_dd").remove();
            	toastr.warning('请重新读取身份证或到人工窗口办理!');
            	 $("#card_read").attr('disabled',true);
            }
        });
    }
    
    function fillTableMedical(basicDataList) {
    	if (null != basicDataList && basicDataList.length > 0) {
    		for (var p = 0; p < basicDataList.length; p++) {
    			var data = basicDataList[p];
    			
    			 var date = new Date();
    			 var outDateFormat = "";
    			 
                 if (data.outTime != null) {
                	 var oDate = new Date(data.outTime);
                	 var oYear = oDate.getFullYear();
                     var oMonth = oDate.getMonth()+1;
                     var oDay = oDate.getDate();
                     
                     outDateFormat = oYear+"-"+oMonth+"-"+oDay;
                 }
                 
                 var ryODateFormat = "";
                 
                 if (data.inTime != null) {
                	 var ry_oDate = new Date(data.inTime);
                	 var ry_oYear = ry_oDate.getFullYear();
                     var ry_oMonth = ry_oDate.getMonth()+1;
                     var ry_oDay = ry_oDate.getDate();

                     ryODateFormat = ry_oYear+"-"+ry_oMonth+"-"+ry_oDay;
                 }
                 
                 var iDays = "";
                 
                 if (oDate != null) {
                	 var dateSpan = date - oDate;
                	 dateSpan = Math.abs(dateSpan);
                     iDays = Math.floor(dateSpan / (24 * 3600 * 1000));
                 }
                 
                 var inNumber = data.inNumber;
                 if (!inNumber) {
                	 inNumber = "";
                 }

                 if(data.length== 1){
                     $("#table_banum").append("<tr id='checkbox_banum_"+data.id+"' onlyId='"+data.onlyId+"' differ='"+iDays+
                    		 "' ><td></td><td id='banum_td_"+data.id+"'><input id='banum_checkbox_"+
                    		 data.id+"' checked='checked'   value='"+data.id+"' name='banum_checkbox' type='radio' style='width:25px; height:25px;'></td><td>"+
                    		 data.id+"</td><td>"+inNumber+"</td><td>"+data.name+"</td><td >"+ryODateFormat+"</td><td >"+outDateFormat+"</td><td>"+data.outDeptName+"</td></tr>");
                 } else {
                     if(p==0) {
                         $("#table_banum").append("<tr id='checkbox_banum_"+data.id+"' onlyId='"+data.onlyId+"' differ='"+iDays
                        		 +"'  ><td></td><td id='banum_td_"+data.id
                        		 +"'><input id='banum_checkbox_"+data.id+"'style='width:25px; height:25px;' checked='checked'  value='"+
                        		 data.id+"' name='banum_checkbox' type='radio'></td><td>"+data.id+"</td><td>"+inNumber+"</td><td>"+data.name+"</td><td >"+
                        		 ryODateFormat+"</td><td>"+outDateFormat+"</td><td>"+data.outDeptName+"</td></tr>");
                     } else {
                         $("#table_banum").append("<tr id='checkbox_banum_"+data.id+"' onlyId='"+data.onlyId+"' differ='"+
                        		 iDays+"'  ><td></td><td id='banum_td_"+data.id+"'><input style='width:25px; height:25px;' id='banum_checkbox_"+
                        		 data.id+"'  value='"+data.id+"' name='banum_checkbox' type='radio'></td><td>"+data.id+"</td><td>"+
                        		 inNumber+"</td><td>"+data.name+"</td><td >"+ryODateFormat+"</td><td>"+outDateFormat+"</td><td>"+data.outDeptName+"</td></tr>");
                     }
                 }
    			
    		}
    		
    	}
    	
    }


    //验证手机号
    function validate_phone(){
        var phone=$("#dy_phone");
        var link_phone=$("#link_phone").val();
        if(!(/^1[34578]\d{9}$/.test(phone.val()))){
            //alert('请输入有效的手机号码！');
            $(document.body).append("<div class='alert fade in alert_warn_'><button type='button' id='btn_warn' onclick='close_warn()' class='close'>&times;</button><div class='warn_div'><font class='font9' id='warn'>请输入有效的手机号码!</font></div>	<div class='warn_div_'><font class='font5' id='warn_'>5</font></div></div>");
            $("#confirm_").attr("disabled", true);
            settime_warn($("#btn_warn"));
            phone.focus();
            return false;
        }else if(link_phone!=phone.val()){
            $(document.body).append("<div class='alert fade in alert_warn_' id='alert_warn_'><button type='button' id='btn_warn' onclick='close_warn()' class='close'>&times;</button><div class='warn_div'><font class='font9' id='warn'>您提供的手机号，与患者留存的手机号不一致。请输入患者住院时留存的手机号，并向该手机拥有者，索取验证码!</font></div>	<div class='warn_div_'><font class='font5' id='warn_'>5</font></div></div>");
            $("#alert_warn_").css("height","150px");
            settime_warn($("#btn_warn"));
            $("#send").attr("disabled", true);
            $("#confirm_").attr("disabled", true);
            return false;
        }

        return true;
    }
    
    //验证短信验证码
    function validation(){
        var idcard=$("#span_idcard").text();
        var phone = $("#mobileNumber").val();
        //直显示验证手机号、身份证的信息
        var validationCode = $("#dy_validation").val();
        
        //验证验证码格式
        if(!(/\d{6}$/.test(validationCode))){
        	toastr.warning("验证码错误!");
            $("#dy_validation").focus();
            return false;
        }
        
        var dataForm = {
        		idCard:idcard,
        		phone:phone,
        		code:validationCode
        };
        
        
        console.log(dataForm);
        
            $.ajax({
            	cache:false,
                type:"POST",
                url:basepath + "/print/validateMessage",
                dataType:"json",
                data:dataForm,
                dataType:"json",
                beforeSend: function(xhr){
                },
                success:function(data){
                	if (data.success) {
                		 console.log("验证手机code成功!");
                		 
                		// acquireBanum(idcard, $("#dy_phone").val());
                         $("#myModal").attr("class","modal_dy ");
                         $("#sfzdiv").attr("class","modal_1 hide");
                         $("#div_key").attr("class","modal_key modal hide");
                         $("#fq_dy").attr("class","button_close");
                         $("#modal_tip").hide();
                   
                         return true;
                	} else {
                		toastr.warning(data.msg);
                        return false;
                	}
                },
                error:function(){
                	toastr.warning('系统异常, 请联系管理员!');
                    return false;
                }
            });
    }

    //发送短信倒计时
    function settime(val) {
        if (countdown <= 0) {
            val.attr("disabled",false);
            val.attr("value","获取验证码");
            countdown = 60;
        } else {
            val.attr("disabled", true);
            val.attr("value","获取验证码(" + countdown + ")");
            countdown--;
            setTimeout(function() {
                settime(val);
            },1000);
        }

    }
    
    //5*60 = 300秒 
    var scanCountDownSecond =  300;
    var timerObj;
    function scanCountDown(orderNum, ipAddress) {
    	console.log("执行验证扫描是否成功, scanCountDownSecond=" + scanCountDownSecond);
    	
    	if (scanCountDownSecond > 0) {
    		var payStatus = checkPayStatus(orderNum, ipAddress);
    		console.log("payStatus=" + payStatus);

    		if (payStatus) {
    			console.log("支付成功");
    			clearTimeout(timerObj); 
    			scanCountDownSecond = 300;
    			$("#btn_close_2").trigger("click"); 
    			
    			//初始化打印机
    			Init();
    			myPrint(ipAddress, orderNum);
    			return;
    		} else {
    			clearTimeout(timerObj); 
        		timerObj = setTimeout(function() {
        			scanCountDownSecond--;
        			$("#scanCountDownSecond").html("支付倒计时："+ scanCountDownSecond + "(秒)");
        			scanCountDown(orderNum, ipAddress);
                 },1000);
    		}
    	} else {
    		//停止
    		toastr.warning("订单支付超时, 请重新勾选打印!");
    		scanCountDownSecond = 300;
    		$("#btn_close_2").trigger("click");
    		
    	}
    }

    function checkPayStatus(orderNum) {
    	var payStatus = false;
        $.ajax({
        	cache:false,
            type:"get",
            url:basepath+"/pay/getprintMrOrderByOrderNum",
            dataType:"json",
            data:{
            	orderNum:orderNum
            },
            async: false,
            beforeSend: function(xhr){
            },
            success:function(data){
            	console.log("checkPayStatus=" + JSON.stringify(data));
            	if (data.success) {
            		payStatus = true;
            	} else {
            		payStatus =  false;
            	}
            },
            error:function(){
            	payStatus =  false;
            }
        });
        
        return payStatus;
    }
    
    function validate_banum(){
        var banum=$("#dy_banum").val();
        if(banum==null||banum==""){
            //alert("请输入病案号");
            $(document.body).append("<div class='alert fade in alert_warn_sfz' id='btn_warn_sfz'><div class='warn_div_sfz'><font class='font18' id='warn'>请输入病案号!</font></div></div>");
            settime_yzm($("#btn_warn_sfz"));
            $("#dy_banum").focus();
            return false;
        }
        return true;
    }


    //打印费用信息
    function printCost() {
    	//病案主键
    	 var basicId = $("input[name='banum_checkbox']:checked").val();
        
        if (!basicId) {
        	toastr.warning('病案唯一标识为空, 请到窗口打印该病案!');
        	return;
        }
        
        console.log("basicId=" + basicId);
        
        //打印的类型
        var printType = $("[name='combination_checkbox'][checked]").val();
        console.log("打印类型=" + printType);
        
        if (!printType) {
        	toastr.warning('请选择打印类型!');
        	return;
        }
        
        //打印份数
        var printCopy =$("#input_fs").val();
        console.log("打印份数=" + printCopy);
        
        payFee(null, basicId, printType, printCopy);
    };


    function payFee(payType, basicId, printType, printCopy){
    	//隐藏生成二维码
    	$("#pay_qr").attr("class", "pay_modal_scan hidden");
    	//清空总费用'
    	$("#hdfy").text("");
    	$("#printTotalFee").val("");
    	//情况打印总页数
    	 $("#printPageTotal").val(""); 
    	
    	var basepath=$("#basepath").val();
    	
    	var dataForm = {
    			mrBasicId:basicId,
    			printType:printType,
    			printCopy:printCopy
    	};
    	
    	 $("body").append("<div id='apDiv'></div>");
    	 $("#myModal").append("<div class='alert_warn_new modal' style='height:75px;overflow:hidden;' id='dy_alert'><div class='warn_div_dq'><font class='font12' id='warn'>请稍后，正在调取病案...</font></div></div>");
    	
        $.ajax({
        	cache:false,
            type:"GET",
            url:basepath+"/print/getPrintFeeAndPrintPage",
            data: dataForm,
            dataType:"json",
            beforeSend: function(xhr){
            },
            success:function(data){
            	$("#apDiv").remove();
            	$(".alert_warn_new").remove();
            	
            	if (data.success) {
            		console.log("打印费用信息" + JSON.stringify(data.data));
            		
            		if (!data.data) {
            			 $(document.body).append("<div class='alert fade in alert_warn_sfz' id='btn_warn_sfz'><div class='warn_div_sfz'><font class='font18' id='warn'>" +
            					"病案暂未扫描，请咨询人工窗口!</font></div></div>");
                        settime_reload($("#btn_warn_sfz"));
            		}
            		
            		var obj = data.data;
            		var cost = obj.printTotalFee;
            		var printPageTotal = obj.totalPrintPage;
            		
            		if (!cost || !printPageTotal) {
            			 $(document.body).append("<div class='alert fade in alert_warn_sfz' id='btn_warn_sfz'><div class='warn_div_sfz'><font class='font18' id='warn'>" +
    					"病案暂未扫描，请咨询人工窗口!</font></div></div>");
            				settime_reload($("#btn_warn_sfz"));
            		}
            		
            		if (cost > 0 && printPageTotal > 0) {
            			$("body").append("<div id='apDiv'></div>");
                        $("#paymentModal").attr("class","payscan");
                        $("#pay_type").attr("class","hidden");
                        $("#pay_balance").attr("class","");
                        $("#pay_balance").show()
                        //打印费用
                        $("#hdfy").text(cost/100);
                        $("#printTotalFee").val(cost);
                        //打印总页数
                        $("#printPageTotal").val(printPageTotal); 
            		} else {
            			 $(document.body).append("<div class='alert fade in alert_warn_sfz' id='btn_warn_sfz'><div class='warn_div_sfz'><font class='font18' id='warn'>" +
    					"病案暂未扫描，请咨询人工窗口!</font></div></div>");
            					settime_reload($("#btn_warn_sfz"));
            		}
            	} else {
            		 $(document.body).append("<div class='alert fade in alert_warn_sfz' id='btn_warn_sfz'><div class='warn_div_sfz'><font class='font18' id='warn'>" +
					"病案暂未扫描，请咨询人工窗口!</font></div></div>");
        					settime_reload($("#btn_warn_sfz"));
            	}
            },
            error:function(){
            	$("#apDiv").remove();
            	$(".alert_warn_new").remove();
                 $(document.body).append("<div class='alert fade in alert_warn_sfz' id='btn_warn_sfz'><div class='warn_div_sfz'><font class='font18' id='warn'>病案暂未扫描，请咨询人工窗口!</font></div></div>");
               settime_reload($("#btn_warn_sfz"));
            }
        });
    }

    //确认打印
    $("#btn_balance").click(function(){
    		$("#pay_balance").hide();
    		$("#pay_type").attr("class", "");
    		
            /*$("#paymentModal").attr("class","hidden");
            $("#paymentVoucher").val($("#pid").val());
            $("#zfts").remove();
            $("#qrcode_fs").val("stop");
            myPrint();*/
    });

    
    
    $("input[id^='pay']").click(function(){
            qrCodeNew();
            
            $("#pay_qr").attr("class","pay_modal_scan");
            $("#pay_qr").html("<div id=\"alipay_loading\" class=\"loadingNew\">正在生成二维码...</div>");
            //testPrint();
     });

    $("input[id^='btn_close']").click(function(){
            $("#paymentModal").attr("class","hidden");
            $("#apDiv").remove();
            
            clearTimeout(timerObj); //消除扫描支付验证定时器
        	scanCountDownSecond = 300;
        });

    
    //后端创建订单, 并生成二维码
    function qrCodeNew(){
    	 $("#printMachineIp").val("");
		 $("#printerName").val("");
    	
    	
    	//alert(timerObj);
    	clearTimeout(timerObj); //消除扫描支付验证定时器
    	scanCountDownSecond = -1;
    	scanCountDownSecond = 300;
     	
        var basepath=$("#basepath").val();
        var obj = $("input[name='banum_checkbox']:checked");
        var name = obj.parent().next().next().next().text();
        var inHospitalTime =  obj.parent().next().next().next().next().text();
        var outHospitalTime =  obj.parent().next().next().next().next().next().text();
        var outDeptName = obj.parent().next().next().next().next().next().next().text();
        var inNumber = obj.parent().next().next().text();
        var mrId= obj.parent().next().text();
        var basicId = obj.val();
        var printType = $("[name='combination_checkbox'][checked]").next().text();
        var printCopy =$("#input_fs").val();
        var printTotalFee = $("#printTotalFee").val();
        var idCard = $("#span_idcard").text();
        var printTotalPages = $("#printPageTotal").val();
        
        var dataForm = {
        		name:name, //姓名
        		mrId:mrId,
        		mrBadicId:basicId, //病案主键
        		printName:printType, //打印类别
        		printNum:printCopy,//打印份数
        		fee:printTotalFee, //打印总费用
        		idNumber:idCard, //身份证号
        		printTotalPages:printTotalPages, //打印总页数
        		printNum:printCopy,
        		idCard:idCard,
        		outDeptName:outDeptName,
        		outTime:outHospitalTime,
        		printName:printType,
        		inNumber:inNumber
        }
        
        console.log("获取支付二维码调用后台接口数据:" + JSON.stringify(dataForm));
        
        $.ajax({
        	cache:false,
            type:"post",
            url:basepath+"/pay/getUnionPayRrcode",
            data:dataForm,
            dataType:"json",
            beforeSend: function(xhr){
            },
            success:function(data){
            	console.log("调用生成二维码接口 " + JSON.stringify(data));
            	if (data.success) {
            		var obj = data.data;
            		
            		 var base64 = obj.qrBase64Str;
            		 var orderNum = obj.orderNum;
            		 var ipAddress = obj.ipAddress;
            		 
            		 var printMachineIp = obj.printMachineIp;
            		 var printerName = obj.printerName;
            		 
            		 $("#printMachineIp").val(printMachineIp);
            		 $("#printerName").val(printerName);
            		 
            		 if (!printMachineIp) {
            			 toastr.warning("无法获取打印机的ip地址, 请咨询人工窗口!");
            			 return;
            		 }
            		 
            		 if (!printerName) {
            			 toastr.warning("无法获取打印机的安装的型号, 请咨询人工窗口!");
            			 return;
            		 }
            		 
            		 
            		 $("#pay_qr").html("<img src=data:image/jpeg;base64,"+base64.replace(/[\n\r]/g,'')+"/><br><font class='font6'>订单号："+orderNum+"</font><br>" +
            		 		"<font class='font6' id='scanCountDownSecond'>支付倒计时："+scanCountDownSecond+"(秒)</font");
            		 scanCountDown(orderNum, ipAddress);
            	} else {
            		$("#pay_qr").attr("class", "pay_modal_scan hidden");
            		toastr.warning(data.msg);
            	}
            
            },
            error:function(){
                 $(document.body).append("<div class='alert fade in alert_warn_sfz' id='btn_warn_sfz'><div class='warn_div_sfz'><font class='font18' id='warn'>病案暂未扫描，请咨询人工窗口!</font></div></div>");
                // settime_reload($("#btn_warn_sfz"));
            }
        });
        
    }


    $("input[id^='fs']").click(
        function(){
            var fsVal=$(this).attr("id").split("_")[1];
            $("#input_fs").val(fsVal);
            //$(this).siblings().css('disabled', 'true');
            //$("#qrcode_div").attr("class","");
            printCost();
            $("#qrcode_fs").val("on");
            //testPrint();
        });

    $("input[id^='printCost']").click(
        function(){
            printCost($(this).attr("id").split("_")[1]);
        });


    String.prototype.myReplace=function(f,e){//吧f替换成e
        var reg=new RegExp(f,"g"); //创建正则RegExp对象
        return this.replace(reg,e);
    }

    //确定验证
    $("#confirm_agent").click(function(){
        $("#confirm_agent").attr("disabled",true);
        $(".alert").remove();
        agentConfirm();
    });
    //身份证验证
    function agentConfirm(){
        var agentCard=$("#nonage_card").val();
        if(checktheform(agentCard)){
            if(checkAge(agentCard)){
                $(document.body).append("<div class='alert fade in' id='btn_warn'><div class='warn_div'><font class='font18' id='warn'>正在读取身份证信息，请稍后...</font></div></div>");
                validateAgent(agentCard);
            }else{
                $(document.body).append("<div class='alert fade in' id='btn_warn'><div class='warn_div'><font class='font18' id='warn'>年龄大于16岁。请返回首页，直接刷本人身份证！</font></div></div>");
                settime_yzm($("#btn_warn"));
                $("#nonage_card").focus();
                return false;
            }
        }else{
            $(document.body).append("<div class='alert fade in' id='btn_warn'><div class='warn_div'><font class='font18' id='warn'>非规范身份证号，请重新输入！</font></div></div>");
            settime_yzm($("#btn_warn"));
            $("#nonage_card").focus();

            return false;
        }
    }

    //
    function validateAgent(idcard){
        $.ajax({
            cache: false,
            type:"POST",
            url:basepath+"/spmr/findBanum",
            dataType:"json",
            data:{idcard:idcard,orgcode:"49557184-0"},
            async: true,
            dataType:"json",
            beforeSend: function(xhr){
                xhr.setRequestHeader(header, token);
            },
            success:function(data){

                if (data.length == 0) {
                    $("#btn_warn").remove();
                    $("#card_read").attr('disabled', true);
                    $(document.body).append("<div class='alert fade in' id='btn_warn'><div class='warn_div'><font class='font18' id='warn'><font class='font18' id='warn'>无归档病案!</font></div></div>");
                    homepage();
                } else {
                    var name = data[0].name;
                    var sex = "";
                    var nation = data[0].nation;
                    var birthday = "";
                    var address = data[0].address;
                    //获取出生日期 
                    birthday = idcard.substring(6, 10) + "-" + idcard.substring(10, 12) + "-" + idcard.substring(12, 14);
                    //获取性别 
                    if (parseInt(idcard.substr(16, 1)) % 2 == 1) {
                        sex = "男";
                        //是男则执行代码 ... 
                    } else {
                        sex = "女";
                        //是女则执行代码 ... 
                    }



                    var flag = 0;

                    var phone = data[0].mobile;

                    if (!(/^1[34578]\d{9}$/.test(phone))) {
                        $("#btn_warn").remove();
                        flag = 1;
                        $("#card_read").attr('disabled', true);
                        $(document.body).append("<div class='alert fade in' id='btn_warn'><div class='warn_div'><font class='font18' id='warn'>预留病人手机号错误!</font></div></div>");
                        homepage();
                        return false;
                    }
                    if (flag == 0) {
                        //验证门诊卡号
                        if ($("#certNumber").val()=='370123198706200013'){
                            idcard="370123198706200013";
                            name=$("#partyName").val();
                        }
                        var ResultMsg = validate_Outpatient(idcard, name);
                        $("#btn_warn").remove();
                        if (ResultMsg == "成功") {
                            $("#name").html(name);
                            $("#header_name_pay").html(name);
                            $("#sex").html(sex);
                            $("#nation_").html(nation);
                            $("#birthday").html(birthday);
                            $("#address").html(address);
                            $("#idcard").html(idcard);
                            $("#table_banum").html("");
                            var banum = data[0].mrId;
                            $("#idcard_").val(idcard);
                            $(document.body).append("<div class='alert fade in' id='btn_warn'><div class='warn_div'><font class='font18' id='warn'>核对未成年人信息后，请发送验证码到预留手机号!</font></div></div>");
                            settime_yzm($("#btn_warn"));
                            $(".oneselftr").attr("class", "oneselftr");
                            $(".agenttr").attr("class", "agenttr hide");
                            $("#nonagediv").attr("class", "hide");
                            $("#div_key").attr("class", "modal_key modal");
                            $("#crdiv").attr("class", "modal_ modal hide");
                            $("#sfzdiv").attr("class", "modal_1");
                            $("#dy_banum").val(banum);
                            $("#header_name_dvd").html(data[0].name);
                            $("#link_phone").val(phone);
                            $("#dy_phone").val(phone);
                            var span_phone = phone.substr(0, 3) + "****" + phone.substr(7, 4);
                            $("#span_phone").html(span_phone);
                            $("#dy_validation").focus();
                            $("#choice").val("dy_validation");
                        } else if (ResultMsg == "error") {
                            $("#btn_warn").remove();
                            $("#confirm_agent").attr("disabled", false);
                            $(document.body).append("<div class='alert fade in' id='btn_warn'><div class='warn_div'><font class='font18' id='warn'>门诊卡信息查询失败，请再次确认获取!</font></div></div>");
                            settime_yzm($("#btn_warn"));
                        } else {
                            $("#btn_warn").remove();
                            $(document.body).append("<div class='alert fade in' id='btn_warn'><div class='warn_div'><font class='font18' id='warn'>未获得身份证绑定门诊卡信息!</font></div></div>");
                            $("#card_read").attr('disabled', false);
                            homepage();
                        }
                    }
                }
            },
            error:function(){
                $("#btn_warn").remove();
                $(document.body).append("<div class='alert fade in' id='btn_warn_sfz'><div class='warn_div'><font class='font18' id='warn'>请重新读取身份证或到人工窗口办理!</font></div></div>");
                $("#card_read").attr('disabled',true);
                settime_reload($("#btn_warn_sfz"));
            }
        });
    }
    
    //返回主界面
    function fanhui() {
    	location = $("#basepath").val() + "/autoprint/printIndex";
    }

});